package com.webside.util;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class HttpUtil {
	public static final String ENCODING="UTF-8";

	public static JSONObject executeHttpPost(String path, String jsonParamStr, HttpParams params,List<NameValuePair> nvps, MultipartEntity multipartEntity) {
		JSONObject resultJson = null;
		HttpClient httpClient = new DefaultHttpClient();
		try {
			HttpPost httpRequest = new HttpPost(path);
			
			httpRequest.setHeader("Content-Type", "application/json; charset=utf-8");
			if(jsonParamStr!=null){
				StringEntity stringEntity = new StringEntity(jsonParamStr);
				httpRequest.setEntity(stringEntity);
			}
			if(params!=null){
				httpRequest.setParams(params);
			}
			if(nvps!=null){
				httpRequest.setEntity(new UrlEncodedFormEntity(nvps));
			}
			if(multipartEntity!=null){
		        httpRequest.setEntity(multipartEntity);
			}
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity httpEntity = httpResponse.getEntity();
				if (httpEntity != null) {
					String result = EntityUtils.toString(httpEntity, ENCODING);
					resultJson =JSONObject.parseObject(result);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return resultJson;
	}

	public static Object executeHttpGet(String path) {
		JSONObject resultJsonObject = null;
		JSONArray resultJsonArray = null;
		HttpClient httpClient = new DefaultHttpClient();
		try {
			HttpGet httpRequest = new HttpGet(path);
			httpRequest.setHeader("Content-Type", "application/json; charset=utf-8");
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity httpEntity = httpResponse.getEntity();
				if (httpEntity != null) {
					String result = EntityUtils.toString(httpEntity, ENCODING);
					if(result.indexOf("{")==0){
						resultJsonObject = JSONObject.parseObject(result);
					}else{
						resultJsonArray= JSONArray.parseArray(result);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		if(resultJsonObject!=null){
			return resultJsonObject;
		}else{
			return resultJsonArray;
		}
		
	}
	public static String getProvince(String ip){
		String returnJson=HttpUtil.executeHttpGet("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ip).toString();
		JSONObject jsonObject=JSONObject.parseObject(returnJson);
		
		return jsonObject.get("province").toString();
	}
	public static String getCity(String ip){
		String returnJson=HttpUtil.executeHttpGet("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ip).toString();
		JSONObject jsonObject=JSONObject.parseObject(returnJson);
		
		return jsonObject.get("city").toString();
	}
	public static void main(String[] args) {
		String returnJson=HttpUtil.executeHttpGet("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.43").toString();
		JSONObject jsonObject=JSONObject.parseObject(returnJson);
		System.out.println(jsonObject.get("city").toString());
		
	}
}
